import csv
import sys
import simcat.client as client
catclient = client.SimCatClient()

lca = client.models.Entity()
lca.name = "Laboratory for Computational Astrophysics"
lca.short_name = "LCA"
lca.address = "9500 Gilman Drive\nMail Code 0424\nLa Jolla, CA  92093-0424"
lca.email = "rwagner@physics.ucsd.edu"
lca.telephone = "+1 (858) 822-4784"
lca.website = "http://lca.ucsd.edu/"
lca.save()
lca = catclient.add(lca)

enzo = client.models.Protocol()
enzo.name = "Enzo"
enzo.short_name = "Enzo"
enzo.description = """Enzo is an adaptive mesh refinement (AMR), grid-based hybrid (hydro + N-Body) code which is designed to do simulations of cosmological structure formation. It uses the algorithms of Berger & Colella to improve spatial and temporal resolution in regions of large gradients, such as gravitationally collapsing objects. The Enzo simulation software is incredibly flexible, and can be used to simulate a wide range of astrophysical and cosmological situations with the physics packages described below (see Features, below). Optionally, Enzo can be used as a non-cosmological hydrodynamics code, or as a pure cosmological N-body code.

Enzo has been parallelized using the MPI message-passing library and can run on any shared- or distributed-memory parallel supercomputer or PC cluster. Simulations using as many as 1024 processors have been successfully carried out on the San Diego Supercomputing Center's Blue Horizon, an IBM SP. """
enzo.referenceURL = "http://lca.ucsd.edu/projects/enzo"
enzo.publisherDID = "enzo"
enzo.status = "valid"
enzo.source = "http://lca.ucsd.edu/software/enzo/v1.0.1/download/enzo-1.0.1.tar.gz"
enzo.type = "simulator"
enzo.version = "1.0"
enzo.save()
# enzo = catclient.add(enzo)

contact = client.models.Contact(resource=enzo, entity=lca, role="publisher")
contact.save()
# catclient.add(contact)
contact = client.models.Contact(resource=enzo, entity=lca, role="owner")
contact.save()
# catclient.add(contact)

grid_cell = client.models.RepresentationObjectType(protocol=enzo)
grid_cell.name='AMR grid cell'
grid_cell.description='Block-structured, cartesian, AMR grid cell.'
grid_cell.multiplicity = '0..*'
grid_cell.label = 'cell'
grid_cell.type = 'hierarchical mesh cell'
grid_cell.save()
# catclient.add(grid_cell)

dm_particle = client.models.RepresentationObjectType(protocol=enzo)
dm_particle.name='Dark matter particle'
dm_particle.description='Point particle representating an amount of dark matter with a given mass.'
dm_particle.multiplicity = '0..*'
dm_particle.label = 'particle'
dm_particle.type = 'point particle'
dm_particle.save()
# catclient.add(dm_particle)

star_particle = client.models.RepresentationObjectType(protocol=enzo)
star_particle.name='Star particle'
star_particle.description='Point particle representating the mass and properties of stars.'
star_particle.multiplicity = '0..*'
star_particle.label = 'particle'
star_particle.type = 'point particle'
star_particle.save()
# catclient.add(star_particle)

hierarchy = client.models.FileType(protocol=enzo,
                                   name='hierarchy',
                                   description='Text file containing a description of all the grids in the simulation.',
                                   mime_type='text/plain')
hierarchy.save()
# catclient.add(hierarchy)

parameter_file = client.models.FileType(protocol=enzo,
                                        name='parameter',
                                        description='Text file containing a list of the input parameters used in the simulation.',
                                        mime_type='text/plain')
parameter_file.save()
# catclient.add(parameter_file)

# Read in parameters from CSV file

datatypes = {"1":"string",
             "2": "int",
             "3": "float",
             "4": "string",
             "5": "boolean",
             "6": "float"}

param_list = csv.reader(open('enzo_parameters.csv'))

for param in param_list:
    try:
        d = {'protocol': enzo,
        'name': param[2],
         'description': param[3]}
        if param[4] != '\N':
            d['datatype'] = datatypes[param[4]]
        if param[5] != '\N':
            d['default'] = param[5]
   
        inputparam = client.models.InputParameter(**d)
        inputparam.save()
    except:
        print param
        sys.exit(-1)

# print enzo.to_json(indent=2)
catclient.add(enzo)
